<?php


namespace app\admin\controller;


use app\common\controller\Adminbase;
use think\Db;

class Route extends Adminbase
{
    protected function initialize()
    {
        parent::initialize();
    }
    public function index()
    {
        if ($this->request->isAjax()) {
            list($page, $limit) = $this->buildTableParames();
            $where[] = ['r.is_del', '=', 0];
            $filter = input('filter');
            if ($filter) {
                $filter = json_decode($filter, true);
                if (isset($filter['id'])) {
                    $where[] = ['r.id', '=', $filter['id']];
                }
                if (isset($filter['status'])) {
                    $where[] = ['r.status', '=', $filter['status']];
                }
                if (isset($filter['type'])) {
                    $where[] = ['r.type', '=', $filter['type']];
                }
                if (isset($filter['hospital'])) {
                    $where[] = ['h.name', 'like', '%'.$filter['hospital'].'%'];
                }
            }
            $_list = Db::name('route')->alias('r')
                ->join('hospital h','r.hospital_id=h.id')
                ->field('r.*,h.name as hospital')
                ->page($page, $limit)
                ->where($where)
                ->order('create_time desc')
                ->order('sort desc')
                ->select();
            foreach ($_list as $k=>$v){
                if ($v['type']==1){
                    $_list[$k]['address']='————————';
                }else{
                    $_list[$k]['province']=$v['province2'];
                    $_list[$k]['city']=$v['city2'];
                }
            }
            $total = Db::name('route')
                ->alias('r')
                ->join('hospital h','r.hospital_id=h.id')
                ->where($where)
                ->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        return $this->fetch();
    }

    /**
     * 状态
     */
    public function setstate()
    {
        $id = $this->request->param('id/d', 0);

        $status = $this->request->param('value/d');

        if (Db::name('route')->where('id', $id)->update(['status' => $status])) {

            $this->success('操作成功！');
        } else {
            $this->error('操作失败！');
        }
    }

    //删除
    public function del()
    {
        $ids = $this->request->param('ids/a', null);
        try {
            //软删除
            foreach ($ids as $id) {
                Db::name('route')->where('id', $id)->update([
                    'is_del' => 1
                ]);
            }
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('删除成功！');
    }

    //增加
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            //todo 路线搜素 路线查重
            if ($data['row']['type']==1){
                if(!$data['row']['province']||!$data['row']['city']){
                    $this->error('选择完整省市!');
                }
            }else{
                if(!$data['row']['address']){
                    $this->error('请填写市内详细地址!');
                }
            }

            $data['row']['create_time'] = time();
            $stmt = Db::name('route')->insertGetId($data['row']);
            if ($stmt) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
        } else {
            $area_list = Db::name('area')->where([
                ['id', '<>', 0],
                ['pid', '=', 0]
            ])->select();
            $this->assign('provice', $area_list);
            return $this->fetch();
        }
    }

    public function edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->Post();

            if ($data['row']['type']==1){
                if(!$data['row']['province']||!$data['row']['city']){
                    $this->error('选择完整省市!');
                }
            }else{
                if(!$data['row']['address']){
                    $this->error('请填写市内详细地址!');
                }
            }

            $stmt = Db::name('route')->update($data['row']);
            if ($stmt) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
        }

        $id = $this->request->param('id/d', 0);
        $data = Db::name('route')->find($id);
        $proviec_list = Db::name('area')->where([
            ['id', '<>', 0],
            ['pid', '=', 0]
        ])->select();

        $provice = Db::name('area')->where(['name' => $data['province']])->find();
        $city_list = Db::name('area')->where([
            'pid' => $provice['id']
        ])->select();


        $provice2 = Db::name('area')->where(['name' => $data['province2']])->find();
        $city_list2 = Db::name('area')->where([
            'pid' => $provice2['id']
        ])->select();
        //医院列表
        $hospital=Db::name('hospital')->where(['is_del'=>0,'city'=>$data['city2']])->select();
        $this->assign([
            'data'=>$data,
            'province'=>$proviec_list,
            'city_list' => $city_list,
            'city_list2' => $city_list2,
            'hospital_list'=>$hospital,
        ]);
        return $this->fetch();
    }


    public function getc_cate()
    {
        $id = input('id');
        $detail = Db::name('area')->where('name', $id)->find();
        $query = db('area')->where('pid', $detail['id'])->select();

        if ($query[0]['name'] == "市辖区") {
            $area_list2 = Db::name('area')->where([
                'pid' => $query[0]['id']
            ])->select();
            foreach ($area_list2 as $k => $v) {
                $query[] = $v;
            }
            unset($query[0]);
        }
        usort($query, function ($a, $b) {
            return $a['id'] - $b['id'];
        });

        return json(['code' => 200, 'data' => $query, 'msg' => '获取成功']);
    }

    //获取医院列表
    public function get_hospit()
    {
        $id = input('id');
        $detail = Db::name('area')->where('name', $id)->find();
        $query = db('hospital')->where([
            'city' => $detail['name'],
            'is_del' => 0
        ])->select();
        return json(['code' => 200, 'data' => $query, 'msg' => '获取成功']);
    }

    public function get_department()
    {
        $id = input('id');
        $query = db('department')->where([
            'hospital_id' => $id,
            'is_del' => 0,
            'pid'=>0
        ])->order('sort desc')->select();
        return json(['code' => 200, 'data' => $query, 'msg' => '获取成功']);
    }
}